<?php

namespace App\Http\Controllers\Admin;

use App\Models\CourseSubject;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;

class CourseSubjectController extends Controller
{
    public function index(){
        $subjects = CourseSubject::paginate(config('constants.page_num'));
        return view('admin.course_subject.index', compact('subjects'));
    }

    public function create(){
        return view('admin.course_subject.create');
    }

    public function store(Requests\CourseRequest $request){
        $result = CourseSubject::create($request->all());
        if ($result){
            exit(json_encode(array('code'=>1, 'url'=>'/admin/subject')));
        }else{
            exit(json_encode(array('code'=>0, 'msg'=>$result)));
        }
    }

    public function edit($id){
        $subject = CourseSubject::findOrFail($id);
        return view('admin.course_subject.edit', compact('subject'));
    }

    public function update(Requests\CourseRequest $request, $id){
        $subject = CourseSubject::findOrFail($id);
        $result = $subject->update($request->all());
        if ($result){
            exit(json_encode(array('code'=>1, 'url'=>'/admin/subject')));
        }else{
            exit(json_encode(array('code'=>0, 'msg'=>$result)));
        }
    }
}
